﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Reflection;
using STS.Model;

namespace STS.ViewModel
{
    public class BaseViewModel : INotifyPropertyChanged
    {
        protected void SaveChangesToDB()
        {
            ModelInstance.DataContext.SubmitChanges();
        }

        protected void SetEntityPropertities<T>(ref T old, T updated)
        {
            Type oldEntityType = old.GetType();
            PropertyInfo[] oldPropertiesList = oldEntityType.GetProperties();

            Type updatedEntityType = updated.GetType();
            PropertyInfo[] updatedPropertiesList = updatedEntityType.GetProperties();

            foreach (PropertyInfo oldProp in oldPropertiesList)
            {
                if (!oldProp.Name.Equals("Id") && !oldProp.PropertyType.IsGenericType)
                {
                    foreach (PropertyInfo updatedProp in updatedPropertiesList)
                    {
                        if (!updatedProp.Name.Equals("Id") && !updatedProp.PropertyType.IsGenericType && updatedProp.Name.Equals(oldProp.Name))
                        {
                            oldProp.SetValue(old, updatedProp.GetValue(updated, null), null);
                            break;
                        }
                    }
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
